Tomcat Interview Questions and Answers
Question: How do I use DataSources with Tomcat?Answer:
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/WallyDB" auth="Container" type="javax.sql.DataSource" username="wally" password="wally" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;" maxActive="8"/> </Context> This example shows how to configure a DataSource for a SQL Server database named mytest located on the development machine. Simply edit the Resource name, driverClassName, username, password, and url to provide values appropriate for your JDBC driver. Access the DataSource in Your Application From a Servlet Here's how you might access the data in a servlet: InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB"); Connection c = ds.getConnection(); ... c.close(); Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/ |
Is it helpful?
Yes
No
Most helpful rated by users:
- What is Tomcat?
- How do you create multiple virtual hosts?
- How do I override the default home page loaded by Tomcat?
- What is Jasper?
- Question : How to communicate between two webservers in two diff systems?